package cn.yujiago.miaosha.mapper;

import cn.yujiago.miaosha.dto.GoodsDTO;
import cn.yujiago.miaosha.entity.Goods;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface GoodsMapper {

    @Select("SELECT * FROM miaosha_goods msg LEFT JOIN goods g ON msg.`goods_id` = g.`id`")
    List<GoodsDTO> selectAllMiaoshaoGoods();

    @Select("SELECT * FROM miaosha_goods msg LEFT JOIN goods g ON msg.`goods_id` = g.`id` WHERE msg.`goods_id` = #{goodsId}")
    GoodsDTO selectGoodsById(@Param("goodsId") Long goodsId);

    @Select("SELECT * FROM miaosha_goods msg LEFT JOIN goods g ON msg.`goods_id` = g.`id` WHERE g.`id` = #{id}")
    GoodsDTO selectById(@Param("id") Long id);

    @Update("UPDATE miaosha_goods SET stock_count = stock_count - 1 WHERE goods_id = #{goodsId} AND stock_count > 0")
    boolean reduceStock(@Param("goodsId") Long goodsId);
}
